<template>
  <div class="network-status-component">
    <el-card class="network-card" :class="{ 'offline': !isOnline }">
      <div class="network-header">
        <el-icon :class="isOnline ? 'online-icon' : 'offline-icon'">
          <component :is="isOnline ? 'Connection' : 'Close'" />
        </el-icon>
        <span class="network-title">
          {{ isOnline ? '网络已连接' : '网络已断开' }}
        </span>
        <el-button 
          v-if="!isOnline" 
          type="primary" 
          size="small" 
          @click="retryConnection"
          :loading="isRetrying"
        >
          重试连接
        </el-button>
      </div>
      
      <div v-if="!isOnline" class="network-suggestions">
        <h4>网络连接问题解决方案：</h4>
        <ul>
          <li>检查WiFi或网络连接</li>
          <li>尝试刷新页面</li>
          <li>检查防火墙设置</li>
          <li>尝试使用手机热点</li>
          <li>联系网络管理员</li>
        </ul>
      </div>
      
      <div v-if="isOnline && lastConnectionTime" class="connection-info">
        <small>上次连接时间: {{ lastConnectionTime }}</small>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import { ElMessage } from 'element-plus'
import { NetworkManager } from '@/utils/offlineSpeechRecognition'

const isOnline = ref(true)
const isRetrying = ref(false)
const lastConnectionTime = ref('')
const networkManager = NetworkManager.getInstance()

const updateNetworkStatus = (online: boolean) => {
  isOnline.value = online
  if (online) {
    lastConnectionTime.value = new Date().toLocaleTimeString()
  }
}

const retryConnection = async () => {
  isRetrying.value = true
  ElMessage.info('正在测试网络连接...')
  
  try {
    const connected = await networkManager.testConnection()
    if (connected) {
      ElMessage.success('网络连接已恢复！')
      isOnline.value = true
      lastConnectionTime.value = new Date().toLocaleTimeString()
    } else {
      ElMessage.error('网络连接仍然不可用')
    }
  } catch (error) {
    ElMessage.error('网络测试失败')
  } finally {
    isRetrying.value = false
  }
}

onMounted(() => {
  isOnline.value = networkManager.getOnlineStatus()
  networkManager.addListener(updateNetworkStatus)
  if (isOnline.value) {
    lastConnectionTime.value = new Date().toLocaleTimeString()
  }
})

onUnmounted(() => {
  networkManager.removeListener(updateNetworkStatus)
})
</script>

<style scoped>
.network-status-component {
  margin-bottom: 20px;
}

.network-card {
  border: 2px solid #e4e7ed;
  transition: all 0.3s ease;
}

.network-card.offline {
  border-color: #f56c6c;
  background-color: #fef0f0;
}

.network-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}

.online-icon {
  color: #67c23a;
  font-size: 18px;
}

.offline-icon {
  color: #f56c6c;
  font-size: 18px;
}

.network-title {
  font-weight: 500;
  flex-grow: 1;
}

.network-suggestions {
  margin-top: 15px;
  padding: 15px;
  background-color: #f5f7fa;
  border-radius: 4px;
}

.network-suggestions h4 {
  margin: 0 0 10px 0;
  color: #606266;
  font-size: 14px;
}

.network-suggestions ul {
  margin: 0;
  padding-left: 20px;
}

.network-suggestions li {
  margin: 5px 0;
  color: #606266;
  font-size: 13px;
}

.connection-info {
  margin-top: 10px;
  text-align: right;
  color: #909399;
}
</style>
